var express = require("express");
var router = express.Router();
var goodsModel = require("../models/goodsModel");
const cartModel = require("../models/cartModel");

/**
 * @api {post} /pro/cart CartAdd
 * @apiDescription 加入购物车
 * @apiName 加入购物车
 * @apiGroup Cart
 * @apiUse token
 * @apiHeader {String} token 用户权限验证
 * @apiBody {String} name 商品名称
 * @apiBody {String} subtitle 商品简介
 * @apiBody {String} min_price 商品价格
 * @apiBody {String} image_url 商品图片
 * @apiBody {String} userid 用户id
 * @apiUse CommonSuccess
 * @apiSuccessExample {json} Success-Response:
 * HTTP/1.1 200 OK
 *  {
 *      "code" : 1,
 *      "msg": "新增成功",
 *      "id":"购物车数据id"
 *  }
 * @apiSampleRequest /pro/cart
 * @apiVersion 1.0.0
 */
router.post("/cart",async function (req, res, next) {
  console.log(req.body);
  let result = await new cartModel(req.body).save()
  console.log(result);
  if(result._id){
    res.send({
      code: 1,
      msg: "新增成功",
      id:result._id
    }) 
  }else{
      res.send({
        code:0,
        msg:'新增失败'
      })
  }
});

/**
 * @api {get} /pro/cart CartGet
 * @apiDescription 查询购物车数据
 * @apiName 购物车查询
 * @apiGroup Cart
 * @apiUse token
 * @apiHeader {String} token 用户权限验证
 * @apiQuery {String} userid 用户id
 * @apiUse CommonSuccess
 * @apiSuccessExample {json} Success-Response:
 * HTTP/1.1 200 OK
 *  {
 *      "code" : 1,
 *      "msg": "查询成功",
 *      "data":[
 *        {
            "_id": "64f08ad10b6c234f5cc5d4eb",
            "userid":"xxx",
            "name": "清新甜妹撸猫感抱枕",
            "subtitle": "软乎乎如撸猫般舒适",
            "min_price": "99",
            "image_url": "https://pimg.wzj.com/wzj/product/spu/5db9c9f0-c6ef-4537-9be1-87ea82e2ad8e.jpg?x-oss-process=style/w400-webp",
            "__v": 0
          }
 *      ]
 *  }
 * @apiSampleRequest /pro/cart
 * @apiVersion 1.0.0
 */

router.get("/cart",async function (req, res, next) {
  let { userid } = req.query;
  console.log(userid);
  let results =await cartModel.find({ userid })
  console.log(results);
  res.send({
    code: 1,
    msg: "查询成功",
    data: results,
  });
});

/**
 * @api {get} /goods GoodsList
 * @apiDescription 分页查询商品列表数据
 * @apiName GoodsList
 * @apiGroup Goods
 * @apiUse token
 * @apiHeader {String} token 用户权限验证
 * @apiQuery {Number} page 页码
 * @apiQuery {Number} pageSize 每页下发数据条数
 * @apiUse CommonSuccess
 * @apiSuccessExample {json} Success-Response:
 * HTTP/1.1 200 OK
 *  {
 *      "code" : 1,
 *      "msg": "加载成功",
 *      "data":[
 *        {
            "_id": "64f08ad10b6c234f5cc5d4eb",
            "name": "清新甜妹撸猫感抱枕",
            "subtitle": "软乎乎如撸猫般舒适",
            "min_price": "99",
            "image_url": "https://pimg.wzj.com/wzj/product/spu/5db9c9f0-c6ef-4537-9be1-87ea82e2ad8e.jpg?x-oss-process=style/w400-webp",
            "__v": 0
          }
 *      ]
 *  }
 * @apiSampleRequest /pro/goods
 * @apiVersion 1.0.0
 */
router.get("/goods", function (req, res, next) {
  let { page, pageSize } = req.query;
  goodsModel
    .find({})
    .skip(page * pageSize)
    .limit(Number(pageSize))
    .then((results) => {
      console.log(results);
      res.send({
        code: 1,
        msg: "查询成功",
        data: results,
      });
    });
});

router.post("/add/goods", function (req, res, next) {
  // 要求登录成功的用户才能访问购物车页面
  console.log(req.body);
  let arr = req.body.list.map(({ name, subtitle, min_price, image_url }) => ({
    name,
    subtitle,
    min_price,
    image_url: image_url + "?x-oss-process=style/w400-webp",
  }));
  goodsModel.insertMany(arr).then((results) => {
    res.send({
      code: 1,
      msg: "录入成功",
    });
  });
});

module.exports = router;
